package at.ac.uniklu.rallyemaster.adapters;

import java.util.ArrayList;
import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import at.ac.uniklu.rallyemaster.RallyeMaster;
import at.ac.uniklu.rallyemaster.domain.TrackingPoint;

public class DBTrackingPoint {
	private SQLiteDatabase db;
	private String[] columns;
	
	
	public DBTrackingPoint() throws SQLiteException{
		this.db = RallyeDBAdapter.getDatabase();
		if(db == null){
			RallyeMaster.logDebug("No Database exists");
		}
		columns = new String[]{DBConstants.KEY_POINT_ID, DBConstants.KEY_POINT_LONG, DBConstants.KEY_POINT_LAT, DBConstants.KEY_POINT_NOTES, DBConstants.KEY_POINT_DISTANCE,
				DBConstants.KEY_POINT_SPEED, DBConstants.KEY_POINT_AVG_SPEED, DBConstants.KEY_POINT_TIME, DBConstants.KEY_POINT_CURVE, DBConstants.KEY_POINT_CURVE_DIRECTION,
				DBConstants.KEY_POINT_CURVE_INTENSION, DBConstants.KEY_POINT_TRACK_ID};
	}
	
	/**
	 * Inserts a trackingPoint to the database
	 * @param _trackingPoint
	 * @return
	 */
	 public long insertTrackingPoint(TrackingPoint _trackingPoint) {
		    
		    ContentValues newPointValues = new ContentValues();      
		    newPointValues.put(DBConstants.KEY_POINT_LONG, _trackingPoint.getLongitude() );
		    newPointValues.put(DBConstants.KEY_POINT_LAT, _trackingPoint.getLatitude() );
		    newPointValues.put(DBConstants.KEY_POINT_NOTES, _trackingPoint.getNotes() ); 
		    newPointValues.put(DBConstants.KEY_POINT_DISTANCE, _trackingPoint.getDistance() );
		    newPointValues.put(DBConstants.KEY_POINT_SPEED, _trackingPoint.getCurrentSpeed() );
		    newPointValues.put(DBConstants.KEY_POINT_AVG_SPEED, _trackingPoint.getAvgSpeed() );
		    newPointValues.put(DBConstants.KEY_POINT_TIME, _trackingPoint.getTime().getTime() ); // saves the time in ms
		    newPointValues.put(DBConstants.KEY_POINT_CURVE, _trackingPoint.getCurve() );
		    newPointValues.put(DBConstants.KEY_POINT_CURVE_DIRECTION, _trackingPoint.getCurveDirection());
		    newPointValues.put(DBConstants.KEY_POINT_CURVE_INTENSION, _trackingPoint.getCurveIntension());
		    newPointValues.put(DBConstants.KEY_POINT_TRACK_ID, _trackingPoint.getTrackId() );
		    
		    RallyeMaster.logDebug("Tracking point "+ _trackingPoint.getLongitude() + "/" + _trackingPoint.getLatitude() +" was saved");
		    return db.insert(DBConstants.DATABASE_TABLE_TRACKINGPOINT, null, newPointValues);
		    
		  }
	 
	  /**
	   * Getting all tracking points from a track
	   * @param trackID is the id from the track
	   * @return ArrayList of TrackingPoints
	   */
	  public ArrayList<TrackingPoint> getTrackingPointsFromTrack(int trackID) {
		  Cursor result = db.query(true, DBConstants.DATABASE_TABLE_TRACKINGPOINT, columns, DBConstants.KEY_POINT_TRACK_ID + "=" + trackID, null, null, null, null, null);
		  ArrayList<TrackingPoint> trackingPoints = new ArrayList<TrackingPoint>();
		  TrackingPoint trackingPoint;
		  RallyeMaster.logDebug("Getting Points from track: " + trackID);
		  result.moveToFirst();
		  for(int i = 0; i<result.getCount(); i++){
			  trackingPoint = new TrackingPoint();
			  trackingPoint.setId(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_ID)));
			  trackingPoint.setLongitude(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_LONG)));
			  trackingPoint.setLatitude(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_LAT)));
			  trackingPoint.setDistance(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_DISTANCE)));
			  trackingPoint.setNotes(result.getString(result.getColumnIndex(DBConstants.KEY_POINT_NOTES)));
			  trackingPoint.setAvgSpeed(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_AVG_SPEED)));
			  trackingPoint.setCurrentSpeed(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_SPEED)));
			  trackingPoint.setTime(new Date(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_TIME))));
			  trackingPoint.setCurve(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_CURVE))>0);
			  trackingPoint.setCurveDirection(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_CURVE_DIRECTION)));
			  trackingPoint.setCurveIntension(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_CURVE_INTENSION)));
			  trackingPoint.setTrackId(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_TRACK_ID)));
			
			  trackingPoints.add(trackingPoint);
			  result.moveToNext();
			 
		  }
		  RallyeMaster.logDebug("Found Points: "+ trackingPoints.size());
		  return trackingPoints;
	}

	  /**
	   * Returns a tracking point by his id
	   * @param pointID ID of the tracking point which should be returned
	   * @return
	   */
	public TrackingPoint getTrackByID(int pointID) {
		Cursor result = db.query(true, DBConstants.DATABASE_TABLE_TRACKINGPOINT, columns, DBConstants.KEY_POINT_ID + "=" + pointID , null, null, null, null, null);
		TrackingPoint trackingPoint = new TrackingPoint();
		result.moveToFirst();
		if(result.getCount() == 1){
			trackingPoint.setId(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_ID)));
			  trackingPoint.setLongitude(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_LONG)));
			  trackingPoint.setLatitude(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_LAT)));
			  trackingPoint.setDistance(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_DISTANCE)));
			  trackingPoint.setNotes(result.getString(result.getColumnIndex(DBConstants.KEY_POINT_NOTES)));
			  trackingPoint.setAvgSpeed(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_AVG_SPEED)));
			  trackingPoint.setCurrentSpeed(result.getDouble(result.getColumnIndex(DBConstants.KEY_POINT_SPEED)));
			  trackingPoint.setTime(new Date(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_TIME))));
			  trackingPoint.setCurve(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_CURVE))>0);
			  trackingPoint.setCurveDirection(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_CURVE_DIRECTION)));
			  trackingPoint.setCurveIntension(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_CURVE_INTENSION)));
			  trackingPoint.setTrackId(result.getInt(result.getColumnIndex(DBConstants.KEY_POINT_TRACK_ID)));
		}
		else{
			return null;
		}
		return trackingPoint;
	}
	
	/**
	 * Removes a tracking point from the database
	 * @param id ID of the tracking point which should be deleted
	 * @return
	 */
	public boolean removeTrackingPoint(int id){
		RallyeMaster.logDebug("Tracking Point deleted with id: " + id);
		return db.delete(DBConstants.DATABASE_TABLE_TRACKINGPOINT, DBConstants.KEY_POINT_ID +" = " + id, null)>0;
	}
	
	/**
	 * Updates a tracking point in the database
	 * @param trackingPoint Tracking point which should be updated
	 * @return
	 */
	public boolean updateTrackingPoint(TrackingPoint trackingPoint){
		ContentValues newPointValues = new ContentValues();      
	    newPointValues.put(DBConstants.KEY_POINT_LONG, trackingPoint.getLongitude() );
	    newPointValues.put(DBConstants.KEY_POINT_LAT, trackingPoint.getLatitude() );
	    newPointValues.put(DBConstants.KEY_POINT_NOTES, trackingPoint.getNotes() ); 
	    newPointValues.put(DBConstants.KEY_POINT_DISTANCE, trackingPoint.getDistance() );
	    newPointValues.put(DBConstants.KEY_POINT_SPEED, trackingPoint.getCurrentSpeed() );
	    newPointValues.put(DBConstants.KEY_POINT_AVG_SPEED, trackingPoint.getAvgSpeed() );
	    newPointValues.put(DBConstants.KEY_POINT_TIME, trackingPoint.getTime().getTime() ); // saves the time in ms
	    newPointValues.put(DBConstants.KEY_POINT_CURVE, trackingPoint.getCurve() );
	    newPointValues.put(DBConstants.KEY_POINT_CURVE_DIRECTION, trackingPoint.getCurveDirection());
	    newPointValues.put(DBConstants.KEY_POINT_CURVE_INTENSION, trackingPoint.getCurveIntension());
	    newPointValues.put(DBConstants.KEY_POINT_TRACK_ID, trackingPoint.getTrackId() );
		
		return db.update(DBConstants.DATABASE_TABLE_TRACKINGPOINT, newPointValues, DBConstants.KEY_POINT_ID +" = " + trackingPoint.getId(), null) > 0;
	}


}
